package com.hong.demo.other.flink;

import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;

/**
 * 按键分区
 */
public class KeyByTest {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> stream = env.fromCollection(Arrays.asList("ab", "abc", "bc", "ab", "bc"));

        KeyedStream<String, String> keyedStream = stream.keyBy(new KeySelector<String, String>() {

            @Override
            public String getKey(String s) throws Exception {
                return s;
            }
        });

        keyedStream.print();

        env.execute();
    }
}
